package com.google.code.cakedroid.context;
//import necessary packages
import com.google.code.cakedroid.base.BaseContext;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;

/**
 *	Dialog Helper Class
 *	It's strongly recommended that you should use the constructor method with the Context parameter!
 *	@author androidyue
 *	Last Modified:2011-12-7    pm 3:08:18
 */
public class DialogHelper extends BaseContext{
	
	
	private AlertDialog.Builder alertDialogBuilder;
	private DialogButtonOnClickListener dialogButtonListener;
	
	/*
	 * constructor
	 */
	public DialogHelper(Context context){
		this.context=context;
	}
	
	
	/*
	 * instantiate  alertDialogBuilder !
	 * It's recommended that call this method when using getDialog methods
	 * @param  titleId			title resource id
	 * @param  message 	message in String
	 */
	private void initAlertDialog(int titleId,String message){
		this.initAlertDialog(titleId);
		this.alertDialogBuilder.setMessage(message);
	}
	
	
	/*
	 * instantiate alertDialogBuilder 
	 * @param titleId  title resource id
	 */
	private void initAlertDialog(int titleId){
		this.alertDialogBuilder=new AlertDialog.Builder(context);
		this.alertDialogBuilder.setTitle(titleId);
	}
	
	
	/*
	 * lazily  instantiate dialogButtonListener
	 */
	private void initDialogButtonListener(){
		if(null==this.dialogButtonListener){
			this.dialogButtonListener=new DialogButtonOnClickListener();
		}
	}
	
	
	/**
	 * get simple  dialog  it contains  title,,message,and one neutral button!
	 * @param titleId    the title resource id
	 * @param messageId   the message resource id
	 * @param neutralButtonTextId   the neutral button resource id
	 * @return   about dialog 
	 */
	public Dialog  getSimpleDialog(int titleId,String message,int neutralButtonTextId ){
		this.initAlertDialog(titleId,message);
		this.initDialogButtonListener();
		this.alertDialogBuilder.setNeutralButton(neutralButtonTextId, this.dialogButtonListener);
		return this.alertDialogBuilder.create();
	}
	
	
	/**
	 * get complex dialog 
	 * @param titleId  							title of dialog
	 * @param message  						title of message
	 * @param positiveTextId  			positive button text resource id
	 * @param negativeTextId			negative button text resource id
	 * @param dialogListener			dialog button onclick listener	
	 * @return												a complex dialog object
	 */
	public  Dialog getComplexDialog(int titleId,String message,int positiveTextId,int negativeTextId,
																				 	DialogInterface.OnClickListener dialogListener){
		this.initAlertDialog(titleId,message);
		this.alertDialogBuilder.setPositiveButton(positiveTextId, dialogListener);
		this.alertDialogBuilder.setNegativeButton(negativeTextId, dialogListener);
		return this.alertDialogBuilder.create();
	}
	
	
	/*
	 * initialize customized dialog by setting title and view
	 */
	private  void  initCustomizedDialog(int titleId,View view){
		this.initAlertDialog(titleId);
		this.alertDialogBuilder.setView(view);
	}
	
	
	/**
	 * Get Simple Customized Dialog
	 * @param titleId   title resource id
	 * @param view     the customized view
	 * @param neutralId    neutral text resource id
	 * @return   customized dialog
	 */
	public Dialog getSimpleCustomizedDialog(int titleId,View view,int neutralId){
		this.initCustomizedDialog(titleId, view);
		this.initDialogButtonListener();
		this.alertDialogBuilder.setNeutralButton(neutralId, dialogButtonListener);
		return this.alertDialogBuilder.create();
	}
	
	
	/**
	 * Get Complex Customized Dialog
	 * @param titleId title resource id
	 * @param view  view to be added
	 * @param positiveTextId  positive text id
	 * @param negativeTextId  negative text id
	 * @param dialogListener   the dialogHandler to handle button onClick events
	 * @return  Dialog 
	 */
	public Dialog getComplexCustomizedDialog(int titleId,View view,int positiveTextId,int negativeTextId,
			DialogInterface.OnClickListener dialogListener){
		this.initCustomizedDialog(titleId, view);
		this.alertDialogBuilder.setPositiveButton(positiveTextId, dialogListener);
		this.alertDialogBuilder.setNegativeButton(negativeTextId, dialogListener);
		return this.alertDialogBuilder.create();
	}
	
	
	/*
	 * dialog button onclick listener 
	 */
	class DialogButtonOnClickListener implements DialogInterface.OnClickListener{

		@Override
		public void onClick(DialogInterface dialog, int which) {
			if(DialogInterface.BUTTON_POSITIVE==which){
				// TODO when positive button is clicked
			}
		}
	}
	
	
	
	
}
